﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.InsertLocationInsideSeam.CApIlisInsertLocInsideSeam
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.InsertLocationInsideSeam;

internal class CApIlisInsertLocInsideSeam
{
  public CApIlisCopyUtils.CopyParametersOptions CopyParameters { get; set; }

  public TxObjectList GetSelectedItems() => TxApplication.ActiveSelection.GetAllItems();

  public void UnselectLocation(ITxObject location)
  {
    TxObjectList txObjectList = new TxObjectList();
    ((Collection<ITxObject>) txObjectList).Add(location);
    TxApplication.ActiveSelection.RemoveItems(txObjectList);
  }

  public bool HasLocationAfter(
    TxRoboticSeamOperation seam,
    TxRoboticSeamLocationOperation seamLocation)
  {
    bool flag = false;
    if (TxRoboticSeamOperation.op_Inequality(seam, (ITxObject) null) && seam.Contains((ITxObject) seamLocation))
    {
      int indexOfChild = seam.GetIndexOfChild((ITxObject) seamLocation);
      int count = seam.Count;
      if (count > 1 && indexOfChild + 1 < count)
        flag = true;
    }
    return flag;
  }

  public bool HasLocationBefore(
    TxRoboticSeamOperation seam,
    TxRoboticSeamLocationOperation seamLocation)
  {
    bool flag = false;
    if (TxRoboticSeamOperation.op_Inequality(seam, (ITxObject) null) && seam.Contains((ITxObject) seamLocation))
    {
      int indexOfChild = seam.GetIndexOfChild((ITxObject) seamLocation);
      if (seam.Count > 1 && indexOfChild != 0)
        flag = true;
    }
    return flag;
  }

  public bool IsSeamLocationOnlyInTheSeam(TxRoboticSeamLocationOperation seamLocation)
  {
    bool flag = false;
    if (TxProjectionEx.GetSeamFromSeamLocation(seamLocation).Count == 1)
      flag = true;
    return flag;
  }

  public ITxContinuousOperation GetContinuousRoboticOperationFromSeam(TxRoboticSeamOperation seam)
  {
    ITxContinuousOperation operationFromSeam = (ITxContinuousOperation) null;
    if (TxRoboticSeamOperation.op_Inequality(seam, (ITxObject) null))
      operationFromSeam = seam.Collection as ITxContinuousOperation;
    return operationFromSeam;
  }

  public TxRoboticSeamLocationOperation GetLocationBefore(
    TxRoboticSeamLocationOperation targetLocation)
  {
    TxRoboticSeamLocationOperation locationBefore = (TxRoboticSeamLocationOperation) null;
    TxRoboticSeamOperation fromSeamLocation = TxProjectionEx.GetSeamFromSeamLocation(targetLocation);
    if (TxRoboticSeamOperation.op_Inequality(fromSeamLocation, (ITxObject) null) && this.HasLocationBefore(fromSeamLocation, targetLocation))
    {
      int indexOfChild = fromSeamLocation.GetIndexOfChild((ITxObject) targetLocation);
      locationBefore = fromSeamLocation.GetChildAt(indexOfChild - 1) as TxRoboticSeamLocationOperation;
    }
    return locationBefore;
  }

  public TxRoboticSeamLocationOperation GetLocationAfter(
    TxRoboticSeamLocationOperation targetLocation)
  {
    TxRoboticSeamLocationOperation locationAfter = (TxRoboticSeamLocationOperation) null;
    TxRoboticSeamOperation fromSeamLocation = TxProjectionEx.GetSeamFromSeamLocation(targetLocation);
    if (TxRoboticSeamOperation.op_Inequality(fromSeamLocation, (ITxObject) null) && this.HasLocationAfter(fromSeamLocation, targetLocation))
    {
      int indexOfChild = fromSeamLocation.GetIndexOfChild((ITxObject) targetLocation);
      locationAfter = fromSeamLocation.GetChildAt(indexOfChild + 1) as TxRoboticSeamLocationOperation;
    }
    return locationAfter;
  }

  public TxRoboticSeamLocationOperation CreateSeamLocationAfter(
    TxRoboticSeamOperation seam,
    TxRoboticSeamLocationOperation relativeLocationInTheSeam,
    TxTransformation newPosition)
  {
    return this.CreateSeamLocation(seam, relativeLocationInTheSeam, newPosition, true);
  }

  public TxRoboticSeamLocationOperation CreateSeamLocationBefore(
    TxRoboticSeamOperation seam,
    TxRoboticSeamLocationOperation relativeLocationInTheSeam,
    TxTransformation newPosition)
  {
    return this.CreateSeamLocation(seam, relativeLocationInTheSeam, newPosition, false);
  }

  public bool Are3PointsOnOneLine(TxVector first, TxVector second, TxVector third)
  {
    double num1 = (third.X - first.X) / (second.X - first.X);
    double num2 = (third.Y - first.Y) / (second.Y - first.Y);
    double num3 = (third.Z - first.Z) / (second.Z - first.Z);
    double num4 = Math.Round(num1, 2);
    double num5 = Math.Round(num2, 2);
    double num6 = Math.Round(num3, 2);
    return num4 == num5 && num4 == num6;
  }

  internal TxRoboticSeamLocationOperation CreateSeamLocation(
    TxRoboticSeamOperation seam,
    TxRoboticSeamLocationOperation relativeLocationInTheSeam,
    TxTransformation newLocatioTrans,
    bool isAfter)
  {
    TxRoboticSeamLocationOperation childAt;
    try
    {
      if (TxProjectionEx.WasSeamOperationProjectedAsARC(seam))
      {
        if (!TxProjectionEx.ProjectARCOnObject(seam, relativeLocationInTheSeam, newLocatioTrans, isAfter))
          return (TxRoboticSeamLocationOperation) null;
      }
      else if (!TxProjectionEx.ProjectOnObject(seam, relativeLocationInTheSeam, newLocatioTrans, isAfter))
        return (TxRoboticSeamLocationOperation) null;
      string str = relativeLocationInTheSeam.Name;
      Match match = new Regex("(\\d+)$").Match(relativeLocationInTheSeam.Name);
      if (match.Success)
        str = relativeLocationInTheSeam.Name.Remove(match.Index);
      string uniqueDisplayName = seam.GetUniqueDisplayName(str);
      int indexOfChild = seam.GetIndexOfChild((ITxObject) relativeLocationInTheSeam);
      int num = !isAfter ? indexOfChild - 1 : indexOfChild + 1;
      childAt = seam.GetChildAt(num) as TxRoboticSeamLocationOperation;
      childAt.Name = uniqueDisplayName;
      this.CopyParametersAndOlp(relativeLocationInTheSeam, childAt);
    }
    catch (Exception ex)
    {
      int num = (int) TxMessageBox.Show(StringTable.ERROR_CREATING_SEAM, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
      return (TxRoboticSeamLocationOperation) null;
    }
    return childAt;
  }

  private void CopyParametersAndOlp(
    TxRoboticSeamLocationOperation referenceLocation,
    TxRoboticSeamLocationOperation newLocation)
  {
    switch (this.CopyParameters)
    {
      case CApIlisCopyUtils.CopyParametersOptions.Robotic:
        CApIlisCopyUtils.CopyParametersOnly((ITxRoboticLocationOperation) referenceLocation, (ITxRoboticLocationOperation) newLocation);
        break;
      case CApIlisCopyUtils.CopyParametersOptions.RoboticAndOLP:
        CApIlisCopyUtils.CopyParametersAndOLP((ITxRoboticLocationOperation) referenceLocation, (ITxRoboticLocationOperation) newLocation);
        break;
    }
  }
}
